package com.tianqu.tms.www.finance.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.IntegralRegisterDto;
import com.tianqu.tms.entity.IntegralRegister;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.finance.viewhelper.AcuMarginViewHelper;

public class AcuMarginSearchResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarProxyRetreatTransAccInputController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try {
			AcuMarginViewHelper view = (AcuMarginViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			//
			Member member = Member.getInstance();
			member = member.findMemberInfoByMemId(view.getMemberId());
			if (!StringUtil.isNullOrBlank(view.getMemberId())) {
				view.setMemberId(member.getMemId() + "");
				view.setPhone(member.getPhone());
				view.setLegal(member.getLegal());
				view.setContactPhone(member.getContactPhone());//返回页面的联系人电话
				view.setContactOwner(member.getContactOwner());	//发挥页面的联系人姓名							
				view.setAccessCardId(member.getAccessCardId());
				view.setAccessCardNo(member.getAccessCardNo());
				view.setCardScore(member.getCardScore());
				view.setRealName(member.getRealName());
				view.setDxtzType("1");
				view.setType(view.getType());
				view.setMargin(member.getAucMargin()+"");
			}
			
			//1：商户会员”+积分处理类型:8、物业保证金缴费
			int chargeType = 0;
			if("1".equals(view.getType())) chargeType = 13;
			else if("2".equals(view.getType())) chargeType = 14;
			IntegralRegister integralRegister = IntegralRegister.getInstance();
			List<IntegralRegisterDto> list = integralRegister.findIntegralByMemId(view.getMemberId(), 1, chargeType);
			int i = 0;
			List<IntegralRegisterDto> r = new ArrayList<IntegralRegisterDto>();
			for (IntegralRegisterDto dto : list) {
				if (i % 2 == 0) dto.setCss("1");
				else dto.setCss("2");
				dto.setInsertTimeStr(DateUtil.DateToString12(dto.getInsertTime()));
				dto.setNum(++i);
				r.add(dto);
			}
			view.setIntegralRegisterList(r);
			//
			controller.commit();
			uc.setAttribute(TmsConstants.VIEWHELP, view);
		} catch (Exception e) {
			logger.error(e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
